﻿using LandManager.Web.Models;
using LandManager.Web.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LandManager.Web.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        UserService userService = new UserService();

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string user_name, string user_password)
        {
            var userInfo = userService.FindUserWithNameAndPwd(user_name, user_password);

            if (userInfo == null)
            {

                ViewBag.Error = "登陆失败，用户名或密码错误!";

                return View();

                //return RedirectToAction("Login"); ;
            }

            this.Session["_UserInfo"] = userInfo;

            return RedirectToAction("Welcome", "Home");
        }

        public ActionResult Index()
        {
            var list = userService.GetList();

            ViewBag.Menu = "系统管理";
            ViewBag.Agency = GetAgencyList();
            return View(list);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            ViewBag.Agency = GetAgencyList();

            ViewBag.Menu = "系统管理";
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(UserInfo model)
        {
            try
            {
                var result = userService.Add(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            var user = userService.FindByID(id);

            ViewBag.Agency = GetAgencyList();
            ViewBag.Menu = "系统管理";
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(UserInfo model)
        {
            try
            {
                var result = userService.Edit(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        private List<Agency> GetAgencyList()
        {
            AgencyService agencyService = new AgencyService();

            var result = agencyService.GetList();

            return result;
        }
    }
}
